package com.hzlj.position.locate.differentiation.positionWarningNotice;

import cn.hutool.core.util.ObjectUtil;
import com.hzlj.position.locate.common.enums.DifferentiationDataType;
import com.hzlj.position.locate.common.model.Position;
import com.hzlj.position.locate.differentiation.DifferentiationService;
import com.hzlj.position.locate.service.DifferentiationConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.hzlj.position.locate.common.enums.DifferentiationDataType.POSITION_WARNING_NOTICE;

/**
 * 定位告警
 */
@Slf4j
@Service
public class PositionWarningNotice implements DifferentiationService {
    @Resource
    private DifferentiationConfigService config;

    @Override
    public DifferentiationDataType getType() {
        return POSITION_WARNING_NOTICE;
    }

    /**
     * 发送消息
     */
    public void noticeJzry(List<Position> positions) {
        if (ObjectUtil.isEmpty(positions)) {
            return;
        }
        //分组
        Map<DifferentiationService, List<Position>> positionsByService = positions.stream()
                .filter(e -> config.getService(this, getType(), e.getJgCode()) != null)
                .collect(Collectors.groupingBy(e -> config.getService(this, getType(), e.getJgCode())));
        //通知
        positionsByService.forEach((k, v) -> {
            ((PositionWarningNotice) k).noticeJzry(v);
        });
    }

    /**
     * 发送消息
     */
    public void noticeGzry(List<Position> positions) {
        if (ObjectUtil.isEmpty(positions)) {
            return;
        }
        //分组
        Map<DifferentiationService, List<Position>> positionsByService = positions.stream()
                .filter(e -> config.getService(this, getType(), e.getJgCode()) != null)
                .collect(Collectors.groupingBy(e -> config.getService(this, getType(), e.getJgCode())));
        //通知
        positionsByService.forEach((k, v) -> {
            ((PositionWarningNotice) k).noticeGzry(v);
        });
    }
}
